<template>
  <div class="department-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <!-- 头部布局 公司信息 -->
        <TreeTools
          :node-data="company"
          :is-root="true"
          @add-depts="addDepts"
        />

        <!-- 树形组件
          data:树形组件数据
          prop:指定树形组件的文件或后代的键名
          default-expand-all: 默认展开
         -->
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <template v-slot="{data}">
            <!-- 组件传值属性名如果有多个单词组成,建议使用-隔开, 但是在组件内部接收时,用小驼峰接收 -->
            <TreeTools
              :node-data="data"
              @del-depts="getDepartments"
              @add-depts="addDepts"
              @edit-depts="editDepts"
            />
          </template>
        </el-tree>
      </el-card>
    </div>

    <!-- 新增部门的弹出框组件 -->
    <!-- <AddDept
      :list="list"
      :node-data="nodeData"
      :show-dialog="showDialog"
      @close-dialog="showDialog=false"
      @add-dept="getDepartments"
    /> -->

    <AddDept
      ref="dept"
      :list="list"
      :node-data="nodeData"
      :show-dialog.sync="showDialog"
      @add-dept="getDepartments"
    />
  </div>
</template>

<script>

import TreeTools from './components/tree-tools.vue'
import AddDept from './components/add-dept.vue'

import { reqGetDepartments } from '@/api/departments'

import { transListToTree } from '@/utils'

export default {
  name: 'Department',
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      departs: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      company: {},

      // 弹出框的控制变量
      showDialog: false,
      nodeData: {},

      list: [],
      loading: false
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    // 获取组织架构数据
    async getDepartments() {
      this.loading = true
      const { data: { depts, companyName }} = await reqGetDepartments()

      this.list = depts
      // 部门树形数据
      this.departs = transListToTree(depts, '')
      // 公司数据
      this.company = { name: companyName, manager: '负责人', id: '' }

      this.loading = false
    },

    // 新增
    addDepts(data) {
      // 1.显示弹出框
      this.showDialog = true
      // 2.把父部门的数据传到弹出框
      this.nodeData = data
    },

    // 编辑
    editDepts(data) {
      // 1.显示弹出框
      this.showDialog = true
      // 2.把自己部门的数据传到弹出框
      this.nodeData = data

      // 3.获取部门的详情  父组件通过ref可以调用子组件内部的方法
      // dialog组件显示或隐藏是异步的
      // this.$nextTick(() => {
      this.$refs.dept.getDepartmentDetail(this.nodeData.id)
      // })
    }

  }
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
